package com.wangjiangfei.service.impl;

import com.wangjiangfei.dao.FreightDao;
import com.wangjiangfei.entity.Freight;
import com.wangjiangfei.entity.Log;
import com.wangjiangfei.service.FreightService;
import com.wangjiangfei.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : wangjiangfei
 * @date : 2025/2/9
 * @description :
 */
@Service
@Slf4j
public class FreightServiceImpl implements FreightService {


    @Autowired
    private FreightDao freightDao;

    @Autowired
    private LogService logService;
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String area) {
            Map<String, Object> map = new HashMap<>();

            // 设置默认页码为1
            page = page == 0 ? 1 : page;
            // 计算偏移量
            int offSet = (page - 1) * rows;

            // 查询当前页的运费方案列表
            List<Freight> freightList = freightDao.getFreightList(offSet, rows, area);

            // 记录日志
            logService.save(new Log(Log.SELECT_ACTION, "分页查询运费方案"));

            // 将总记录数和当前页的记录放入返回的Map中
            map.put("total", freightDao.getFreightCount(null));
            map.put("rows", freightList);

            return map;
        }

    @Override
    public void addFreight(Freight freight) {
        freightDao.addFreight( freight);
        logService.save(new Log(Log.INSERT_ACTION, "新增运费方案"));
    }

    @Override
    public void updateFreight(Freight freight) {
        freightDao.updateFreight(freight);
        logService.save(new Log(Log.UPDATE_ACTION, "修改运费方案"));
    }

    @Override
    public void deleteFreight(Integer freightId) {
        freightDao.deleteFreight(freightId);
        logService.save(new Log(Log.DELETE_ACTION, "删除运费方案"));
    }
}
